package com.xf.web.controller.sys;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xf.pm.common.PageData;
import com.xf.pm.common.PageForm;
import com.xf.pm.util.ResultMsg;
import com.xf.sys.entity.SysRole;
import com.xf.sys.service.ISysResourcesRoleService;
import com.xf.sys.service.ISysRoleService;

/**
 * xf-web
 * RoleController.java
 * author:lb
 * 2017年7月3日
 */
@Controller
@RequestMapping("/sysRole")
public class SysRoleController {
	
	private final Logger logger =  LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ISysRoleService sysRoleService;
	
	@Autowired
	private ISysResourcesRoleService sysResourcesRoleService;
	
	/**
	 * 初始化界面
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list()
	{
		return "sys/role/list";
	}
	
	/**
	 * 角色列表查询
	 * @param form
	 * @param sysRole
	 * @return
	 */
	@RequestMapping(value = "/selectSysRoleList")
	public @ResponseBody PageData selectSysRoleList(PageForm form,SysRole sysRole)
	{
		return sysRoleService.selectSysRoleList(form,sysRole);
	}
	
	/**
	 * 新增角色信息
	 * @param SysRole
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody ResultMsg addSysRole(SysRole SysRole){
		ResultMsg msg=new ResultMsg();
        try {
        	if(null!=SysRole){
        		int result=sysRoleService.insertSysRole(SysRole);
        		if(result==1){
        			msg.setMsg("添加角色成功");
            		msg.setSuccess(true);
        		}else{
        			msg.setMsg("添加角色失败");
            		msg.setSuccess(false);
        		}
            }
		} catch (Exception e) {
			logger.error("添加角色结束异常");
		    e.printStackTrace();
		}
        return msg;
	}
	
	/**
	 * 删除角色
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	public @ResponseBody ResultMsg removeSysRole(Integer[] ids){
		ResultMsg msg=new ResultMsg();
        try {
        	if(null!=ids&&ids.length>0){
        		int result=sysRoleService.deleteSysRole(ids);
        		if(result>0){
        			msg.setMsg("删除角色成功");
            		msg.setSuccess(true);
        		}else{
        			msg.setMsg("删除角色失败");
            		msg.setSuccess(false);
        		}
            }
		} catch (Exception e) {
			logger.error("删除角色结束异常");
		    e.printStackTrace();
		}
		return msg;
	}
	
	/**
	 * 修改角色信息
	 * @param SysRole
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody ResultMsg updateSysRole(SysRole SysRole){
		ResultMsg msg=new ResultMsg();
        try {
        	if(null!=SysRole){
            	int result=sysRoleService.updateSysRole(SysRole);
            	if(result==1){
            		msg.setMsg("修改角色成功");
            		msg.setSuccess(true);
            	}else{
            		msg.setMsg("修改角色失败");
            		msg.setSuccess(false);
            	}
            }
		} catch (Exception e) {
			logger.error("修改角色结束异常");
		    e.printStackTrace();
		}
        return msg;
	}
	
	/**
	 * 查询当个角色信息
	 * @param roleId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit")
	public String edit(Integer roleId, ModelMap model)
	{
		if (roleId != null)
		{
			model.addAttribute("dto", sysRoleService.selectSysRole(roleId));
		}
		return "sys/role/edit";
	}
	
	/**
	 * 获取所有角色
	 * @return
	 */
	@RequestMapping(value = "/selectSysRole")
	public @ResponseBody List<SysRole> selectSysRole(){
		List<SysRole> list=new ArrayList<SysRole>();
		try {
			list=sysRoleService.selectSysRoleList();
		} catch (Exception e) {
			logger.error("获取所有角色结束异常");
		    e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 跳转到设置权限界面
	 * @param roleId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/permission")
	public String permission(Integer roleId, ModelMap model)
	{
		if (roleId != null)
		{
			model.addAttribute("dto", sysRoleService.selectSysRole(roleId));
		}
		return "sys/role/permission";
	}
	
	/**
	 * 给角色设置权限信息
	 * @param SysRole
	 * @return
	 */
	@PostMapping("setPermission")
	public @ResponseBody ResultMsg setPermission(Integer[] resourceId,Integer roleId){
		ResultMsg msg=new ResultMsg();
        try {
        	if(null!=resourceId&&resourceId.length>=0&&null!=roleId){
            	int result=sysResourcesRoleService.updateSysResourceRole(resourceId, roleId);
            	if(result>0){
            		msg.setMsg("操作成功");
            		msg.setSuccess(true);
            	}else{
            		msg.setMsg("操作失败");
            		msg.setSuccess(false);
            	}
            }
		} catch (Exception e) {
			logger.error("操作异常");
		    e.printStackTrace();
		}
        return msg;
	}

}
